package com.hotau.account;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 绑定预付卡
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-11
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
public class BindPaymentAdvanceFragment extends Fragment implements
		OnClickListener {

	private Spinner payment_advance_sp_type = null;
	private EditText payment_advance_et_card = null;
	private EditText payment_advance_et_password = null;
	private EditText payment_advance_et_pay_password = null;
	private Button payment_advance_btn_submit = null;
	private List<String> advance_type_names = new ArrayList<String>();
	private List<String> advance_type_values = new ArrayList<String>();
	
	
	private PaymentAdvanceManageFragment parentFragment = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		parentFragment = (PaymentAdvanceManageFragment)getArguments().get("parentFragment");
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
		HttpRequestAsynTask payment_advance_type_task = new HttpRequestAsynTask(getActivity(),"正在加载数据..."){
			@Override
			protected void doPostExecute(String result) {
				// TODO Auto-generated method stub
				HotauActivity csmbpActivity = (HotauActivity) getActivity();
				//DialogUtil.showDialog(getActivity(), "", result);
				try {
					JSONObject data = new JSONObject(result);
					if(data != null && data.getInt("result") == 1){
						JSONArray array = data.getJSONArray("channelList");
						for(int i = 0;i<array.length();i++){
							advance_type_values.add(array.getJSONObject(i).getString("cardChannelCode"));
							advance_type_names.add(array.getJSONObject(i).getString("cardChannelName"));
						}
						ArrayAdapter<String> cardTypes = new ArrayAdapter<String>(getActivity(), R.layout.account_payment_advance_sp_layout,advance_type_names);
						cardTypes.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
						payment_advance_sp_type.setAdapter(cardTypes);
					}else{
						//获取绑定卡渠道失败
						DialogUtil.showDialog(getActivity(), "", data.getString("errorMsg"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					DialogUtil.showDialog(getActivity(), "", "获取绑定卡渠道返回数据出错 ");
				}
			}
		};
		try {
			StringBuilder data = new StringBuilder();
			data.append("Form:token="+MemberService.getToken(getActivity()));
			data.append(";terminalCode="+ HotauConstants.getTerminalCode(getActivity()));
			payment_advance_type_task.execute(HotauConstants.ACCOUNT_QUERY_CARD_CHANNEL_INFO,data.toString(),HttpUtil.UTF8_ENCODING);
		} catch (Exception e) {
			// TODO: handle exception
			DialogUtil.showDialog(getActivity(), "", "获取绑定卡渠道返回数据出错 ");
		}
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.account_bind_payment_advance, container,false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_payment_advance_btn_submit:
			try {
				
			
				//创建预付费
				long itemId = payment_advance_sp_type.getSelectedItemId();
				if(itemId < 0){
					DialogUtil.showDialog(getActivity(), "", "没有选择卡类型或加载失败");
					return;
				}
				if("".equals(payment_advance_et_card.getText().toString().trim())){
					DialogUtil.showDialog(getActivity(), "", "预付卡卡号不能为空");
					return;
				}
				if("".equals(payment_advance_et_password.getText().toString())){
					DialogUtil.showDialog(getActivity(), "", "预付卡密码不能为空");
					return;
				}
				if("".equals(payment_advance_et_pay_password.getText().toString())){
					DialogUtil.showDialog(getActivity(), "", "支付密码不能为空");
					return;
				}
				HttpRequestAsynTask payment_advance_type_task = new HttpRequestAsynTask(getActivity()){
					@Override
					protected void doPostExecute(String result) {
						// TODO Auto-generated method stub
						final HotauActivity csmbpActivity = (HotauActivity) getActivity();
						//DialogUtil.showDialog(getActivity(), "", result);
						try {
							JSONObject data = new JSONObject(result);
							if(data != null && data.getInt("result") == 1){
								//绑定成功
	//							csmbpActivity.pushFragment("信息提示", new MessagePromptFragment(true, "绑定成功"));
								DialogUtil.showDialog(csmbpActivity, "", "绑定成功",new Executable() {
									@Override
									public void execute() {
										// TODO Auto-generated method stub
										parentFragment.passValue(0, "0");
										csmbpActivity.popFragment();
									}
								});
							}else{
								//绑定卡失败
	//							csmbpActivity.pushFragment("信息提示", new MessagePromptFragment(true, data.getString("errorMsg")));
								DialogUtil.showDialog(csmbpActivity, "", data.getString("errorMsg"));
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							DialogUtil.showDialog(getActivity(), "", "绑定卡返回数据出错 ");
						}
					}
				};
				StringBuilder data = new StringBuilder();
				data.append("Form:token="+MemberService.getToken(getActivity()));
				data.append(";cardChannelCode="+advance_type_values.get((int)itemId));
				data.append(";cardChannelName="+advance_type_names.get((int)itemId));
				data.append(";cardNo="+payment_advance_et_card.getText().toString());
				data.append(";cardPwd="+payment_advance_et_password.getText().toString());
				data.append(";payPwd="+payment_advance_et_pay_password.getText().toString());
				data.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
				payment_advance_type_task.execute(HotauConstants.ACCOUNT_BIND_CARD_INFO,data.toString(),HttpUtil.UTF8_ENCODING);
			} catch (Exception e) {
				// TODO: handle exception
				DialogUtil.showDialog(getActivity(), "", "绑定预付费卡失败 ");
			}
			break;
		default:
			break;
		}
	}
	
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget() {
		payment_advance_sp_type = (Spinner) getActivity().findViewById(R.id.account_payment_advance_sp_type);
		payment_advance_et_card = (EditText) getActivity().findViewById(R.id.account_payment_advance_et_card);
		payment_advance_et_password = (EditText) getActivity().findViewById(R.id.account_payment_advance_et_password);
		payment_advance_btn_submit = (Button) getActivity().findViewById(R.id.account_payment_advance_btn_submit);
		payment_advance_et_pay_password = (EditText) getActivity().findViewById(R.id.account_payment_advance_et_pay_password);
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		payment_advance_btn_submit.setOnClickListener(this);
	}

}
